package mi.payment.utils.transaction;

import android.content.Context;
import android.util.Base64;

import java.util.HashMap;
import java.util.Map;

import lainey.ui.utils.LogUtil;
import mi.payment.Constant;
import mi.payment.admin.RKI;
import mi.payment.bean.db.TradeData;
import mi.payment.utils.AllianceUtil;
import mi.payment.utils.AsciiUtil;
import mi.payment.utils.ByteUtil;
import mi.payment.utils.iso8583.EncodePackage;
import mi.payment.utils.iso8583.FieldUtils;
import mi.payment.utils.iso8583.MsgField;
import mi.payment.utils.iso8583.Utils;
import mi.payment.utils.secure.TerminalSecure;

public class InitMachineTask extends TransactionTask<TradeData, Void, TradeData> {

    public InitMachineTask(Context context) {
        super(context, false);
    }

    @Override
    protected void onBegin(TradeData[] params) {
        tradeData = params[0];
        dbBean = tradeData.getPayDetail();
    }

    @Override
    protected TradeData appointResult() {
        return tradeData;
    }

    @Override
    protected Map<String, MsgField> preAssembly() {
        return EncodePackage.preAssemblyBase("2020010000C00080");
    }

    @Override
    protected byte[] changeAndAssembly(Map<String, MsgField> fieldMap) {
        dbBean.setProcessCode("950001");
        dbBean.TerminalDate = System.currentTimeMillis() / 1000;

        Map<String, MsgField> map = FieldUtils.fillEachFieldValue(fieldMap, tradeData);

        MsgField f024 = map.get("F024");
        f024.setDataStr("263");

        MsgField f057 = map.get("F057");
        String f057_value = "3033";                                                                 // Encryption Algorithm
        f057_value += "34";                                                                         // Key Management
        f057_value += "34";                                                                         // Mac Algorithm
        f057_value += "303101";                                                                     // Command Code
        f057_value += "0030003000300030003000300030003000300030003000300030003000300030003000300030003001";
        f057_value += "0030003000300030003000300030003000300030003000300030003000300030003000300030003001";
        f057_value += "303101";                                                                     // APP ID
        // f057_value += "303034363800300030003000300030003000300030003000300030003000300030003001";   // Device Serial No.

        String smartCardSerialNo = AllianceUtil.encode250(RKI.mSessionAppID);
        LogUtil.e(Constant.TAG, "appID: " + smartCardSerialNo);

        String sessionKey = AllianceUtil.encode250(RKI.mSessionKey);
        LogUtil.e(Constant.TAG, "sessionKey: " + sessionKey);

        String toAscii = AsciiUtil.stringToAscii(RKI.mSessionKcv);
        String sessionKcv = AllianceUtil.encode250(toAscii);
        LogUtil.e(Constant.TAG, "sessionKcv: " + sessionKcv);

        f057_value += smartCardSerialNo + "01";
        f057_value += sessionKey + "01";
        f057_value += sessionKcv;

        byte[] bytes = ByteUtil.hexStr2Bytes(f057_value);
        String encodeToString = Base64.encodeToString(bytes, Base64.NO_WRAP);
        LogUtil.e(Constant.TAG, "encodeToString: " + encodeToString);

        f057.setDataStr(encodeToString);

        return EncodePackage.assembly(map, "0800", TerminalSecure.PROCESSREQUEST_NONE);
    }

    @Override
    protected void onUnpacked(HashMap<String, MsgField> resp) {
        MsgField f057 = resp.get("F057");
        if (f057 != null) {
            String dataStr = f057.getDataStr();
            byte[] decodeBytes = Base64.decode(dataStr, Base64.NO_WRAP);
            String hexStr = ByteUtil.bytes2HexStr(decodeBytes);
            LogUtil.e(Constant.TAG, "hexStr: " + hexStr);
            RKI.mEncryptedData = AllianceUtil.decode250(hexStr);
            LogUtil.e(Constant.TAG, "decode250: " + RKI.mEncryptedData);
        }
    }

    @Override
    public byte[] regroupPackPreSend(byte[] sendBytes) {
        String log = "重组之前：" + Utils.byte2HexStr(sendBytes);
        LogUtil.e(Constant.TAG, log);

        byte[] outBytes = new byte[sendBytes.length - 6];
        System.arraycopy(sendBytes, 2, outBytes, 2, 5);
        System.arraycopy(sendBytes, 13, outBytes, 7, sendBytes.length - 13);

        int hig = (outBytes.length - 2) / 256;
        int low = (outBytes.length - 2) % 256;
        outBytes[0] = (byte) hig;
        outBytes[1] = (byte) low;

        log = "重组之后:" + Utils.byte2HexStr(outBytes);
        LogUtil.e(Constant.TAG, log);

        return outBytes;
    }

    @Override
    public byte[] regroupPackOnResponse(byte[] receiveBytes) {
        String log = "重组之前:" + Utils.byte2HexStr(receiveBytes);
        LogUtil.e(Constant.TAG, log);

        byte[] outBytes = new byte[receiveBytes.length + 6];

        byte[] headBytes = Utils.hexStr2Bytes("888888888888");

        System.arraycopy(receiveBytes, 0, outBytes, 0, 7);
        System.arraycopy(headBytes, 0, outBytes, 7, headBytes.length);
        System.arraycopy(receiveBytes, 7, outBytes, 13, receiveBytes.length - 7);

        log = "重组之后:" + Utils.byte2HexStr(outBytes);
        LogUtil.e(Constant.TAG, log);

        return outBytes;
    }


}
